package org.jeecg.modules.demo.expenseAllocation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.expenseAllocation.entity.YxExpenseAllocation;
import org.jeecg.modules.demo.expenseAllocation.mapper.YxExpenseAllocationMapper;
import org.jeecg.modules.demo.expenseAllocation.service.IYxExpenseAllocationService;
import org.jeecg.modules.demo.socost.entity.SoCost;
import org.jeecg.modules.demo.socost.mapper.SoCostMapper;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;

import static java.math.BigDecimal.ROUND_FLOOR;

/**
 * @Description: yx_expense_allocation
 * @Author: jeecg-boot
 * @Date:   2021-12-10
 * @Version: V1.0
 */
@Service
public class YxExpenseAllocationServiceImpl extends ServiceImpl<YxExpenseAllocationMapper, YxExpenseAllocation> implements IYxExpenseAllocationService {
    @Resource
    private YxExpenseAllocationMapper expenseAllocationMapper;
    @Resource
    private WarehouseOrderMapper warehouseOrderMapper;
    @Resource
    private SoCostMapper soCostMapper;

    @Override
    public String getMaxCode(){
        return expenseAllocationMapper.getMaxCode();
    }

    @Override
    @Transactional
    public boolean ftSoCost(WarehouseOrder warehouseOrder, SoCost soCost, YxExpenseAllocation yxExpenseAllocation){
        try{
            WarehouseOrder warehouseOrder1 = updateWOrder(warehouseOrder,yxExpenseAllocation);
            yxExpenseAllocation.setFtwsje(warehouseOrder1.getFtwsje());
            soCost.setSyje(new BigDecimal(BigDecimalUtil.sub(soCost.getSyje().doubleValue(),yxExpenseAllocation.getPsAmount().doubleValue())));
            if(soCost.getSyje().compareTo(new BigDecimal(0.00))==0){
                soCost.setApportionstate(2);
            }else {
                soCost.setApportionstate(1);
            }
            warehouseOrderMapper.updateById(warehouseOrder1);
            soCostMapper.updateById(soCost);
            expenseAllocationMapper.insert(yxExpenseAllocation);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
    @Override
    @Transactional
    public boolean delExpenSeAllocation(YxExpenseAllocation expenseAllocation){
        try{
            WarehouseOrder order = warehouseOrderMapper.getByWoNo(expenseAllocation.getWoNo());
            WarehouseOrder warehouseOrder = deleteWOrder(order,expenseAllocation);
            SoCost soCost = soCostMapper.getByFtNo(expenseAllocation.getSocCostNo());
            soCost.setSyje(new BigDecimal(BigDecimalUtil.add(soCost.getSyje().doubleValue(),expenseAllocation.getPsAmount().doubleValue())));
            BigDecimal jine = soCost.getSyje().setScale(3,ROUND_FLOOR);
            if(jine.compareTo(soCost.getPsAmount())==0){
                soCost.setApportionstate(0);
            }else {
                soCost.setApportionstate(1);
            }
            soCostMapper.updateById(soCost);
            warehouseOrderMapper.updateById(warehouseOrder);
            expenseAllocationMapper.deleteById(expenseAllocation.getId());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    /**
     *  费用分摊时 修改 入库单无税单价等等
     * @param warehouseOrder
     * @return
     */
    public WarehouseOrder updateWOrder(WarehouseOrder warehouseOrder,YxExpenseAllocation expenseAllocation){
        //原无税单价
        Double  wsdj = warehouseOrder.getWsdj().doubleValue();
        //初始费用金额
        Double csfeiyong = warehouseOrder.getFyje().doubleValue();
        //分摊金额
        Double jine = expenseAllocation.getPsAmount().doubleValue();
        //分摊费用金额
        warehouseOrder.setFyje(new BigDecimal(BigDecimalUtil.add(csfeiyong,jine)));
        //初始税率
        Double sl = warehouseOrder.getPsTaxrat().doubleValue();
        //数量
        Double amount = warehouseOrder.getQoQuantity().doubleValue();
        //初始总成本
        Double cschengben =warehouseOrder.getFttotalcost().doubleValue();
        //累计分摊费用金额
        Double ljjine = BigDecimalUtil.add(csfeiyong,jine);
        //分摊后无税单价
        Double  bdwsdj = BigDecimalUtil.div(amount,ljjine,7);
        Double  ftwsdj = BigDecimalUtil.add(bdwsdj,wsdj);
        warehouseOrder.setFtwsdj(new BigDecimal(ftwsdj));
        //分摊后无税金额
        Double ftwsje = BigDecimalUtil.mul(ftwsdj,amount);
        warehouseOrder.setFtwsje(new BigDecimal(ftwsje));
        //税率转为小数，如0.13
        Double  sl1 = BigDecimalUtil.div(100.00,sl,7);
        Double  shuilv = BigDecimalUtil.add(sl1,1.00);
        //分摊后含税单价
        Double fthsdj = BigDecimalUtil.mul(ftwsdj,shuilv);
        warehouseOrder.setFthsdj(new BigDecimal(fthsdj));
        //分摊后含税金额
        Double fthsje = BigDecimalUtil.mul(fthsdj,amount);
        warehouseOrder.setFthsje(new BigDecimal(fthsje));
        //分摊后总成本
        warehouseOrder.setFttotalcost(new BigDecimal(BigDecimalUtil.add(cschengben,jine)));
        //分摊后得税额
        Double ftse = BigDecimalUtil.sub(fthsje,ftwsje);
        warehouseOrder.setFtse(new BigDecimal(ftse));
        return warehouseOrder;
    }

    public WarehouseOrder deleteWOrder(WarehouseOrder warehouseOrder,YxExpenseAllocation expenseAllocation){
        //原无税单价
        Double  wsdj = warehouseOrder.getFtwsdj().doubleValue();
        //初始费用金额
        Double csfeiyong = warehouseOrder.getFyje().doubleValue();
        //分摊金额
        Double jine = expenseAllocation.getPsAmount().doubleValue();
        //分摊费用金额
        warehouseOrder.setFyje(new BigDecimal(BigDecimalUtil.sub(csfeiyong,jine)));

        //数量
        Double amount = warehouseOrder.getQoQuantity().doubleValue();
        //初始总成本
        Double cschengben =warehouseOrder.getFttotalcost().doubleValue();

        Double  bdwsdj = BigDecimalUtil.div(amount,jine,7);
        Double  ftwsdj = BigDecimalUtil.sub(wsdj,bdwsdj);
        warehouseOrder.setFtwsdj(new BigDecimal(ftwsdj));

        //初始税率
        Double sl = warehouseOrder.getPsTaxrat().doubleValue();
        //分摊后无税金额
        Double ftwsje = BigDecimalUtil.mul(ftwsdj,amount);
        warehouseOrder.setFtwsje(new BigDecimal(ftwsje));


        //税率转为小数，如0.13
        Double  sl1 = BigDecimalUtil.div(100.00,sl,7);
        Double  shuilv = BigDecimalUtil.add(sl1,1.00);
        //分摊后含税单价
        Double fthsdj = BigDecimalUtil.mul(ftwsdj,shuilv);
        warehouseOrder.setFthsdj(new BigDecimal(fthsdj));
        //分摊后含税金额
        Double fthsje = BigDecimalUtil.mul(fthsdj,amount);
        warehouseOrder.setFthsje(new BigDecimal(fthsje));
        //分摊后总成本
        warehouseOrder.setFttotalcost(new BigDecimal(BigDecimalUtil.sub(cschengben,jine)));
        //分摊后得税额
        Double ftse = BigDecimalUtil.sub(fthsje,ftwsje);
        warehouseOrder.setFtse(new BigDecimal(ftse));
        return warehouseOrder;
    }
}
